package org.feiye.studyday25;

import java.io.*;

public class BufferWriterTest {

	public static void main(String[] args) {
		InputStream fis = null;
		Reader isr = null;
		BufferedReader r = null;
		Writer fw = null, w = null;
		try {
			fis = new FileInputStream("F:\\叶飞io测试\\字符输入输出流\\pet.template");
			isr = new InputStreamReader(fis, "utf-8");
			r = new BufferedReader(isr);
			String line = null;
			StringBuffer sbf = new StringBuffer();
			while ((line = r.readLine()) != null) {
				sbf.append(line);
			}
			System.out.println("替换前：" + sbf);
			fw = new FileWriter("F:\\叶飞io测试\\字符输入输出流\\text.txt");
			w = new BufferedWriter(fw);
			/* 替换内容 */
			String newString = sbf.toString().replace("{name}", "欧欧");
			newString = newString.replace("{type}", "狗狗");
			newString = newString.replace("{master}", "李伟");
			System.out.println("替换后：" + newString);
			w.write(newString); // 写入文件
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			// 关闭 reader 和 writer.
			if (w != null)
				try {
					w.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			if (fw != null)
				try {
					fw.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			if (r != null)
				try {
					r.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			if (isr != null)
				try {
					isr.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			if (fis != null)
				try {
					fis.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
		}

	}
}
